import {
	createApp
} from 'vue'
import App from './App.vue'
import API from "@/config/api"
import uView from './uni_modules/vk-uview-ui';
import store from './store'
import {
	jump,
	jumpToTabbar,
	goBack,
	getJumpParam,
	goExternalLink
} from './utils/jumpUtil'
import loadingPlugin from './utils/plugIn/globalLoading'
import GlobalLoading from "@/components/diyLoading/index.vue";
import SlideLoading from "./components/SlideLoading/index.vue";
import SubmitButton from "./components/SubmitButton/index.vue";
import ReturnTop from "./components/ReturnTop/index.vue";
import UvSkeleton from "@/components/uv-skeleton/uv-skeleton.vue";

const $goToBqCenter = () => {
	if (checkLogin()) {
		let userInfo = uni.getStorageSync('storage_userInfo')
		if (!userInfo.authed) {
			uni.showModal({
				title: "实名认证",
				content: '用户还未认证，请先实名认证',
				cancelText: '不认证',
				confirmText: '去认证',
				success: (res) => {
					if (res.confirm) {
						saveLasePage()
						uni.navigateTo({
							url: '/pages_category_page2/userModule/auth'
						})
					}
				}
			})
		} else{
			let loginAuthInfo = uni.getStorageSync('storage_key')
			goExternalLink(`${API.bqCenterUrl}?token=${loginAuthInfo.token}&api=${API.baseApi}/openApi/`)
		}
	}
}
const isLogin = () => {
	let token = uni.getStorageSync('storage_key')
	return !(JSON.stringify(token) === '{}' || token.buyerUserId === undefined)
}

const jumpLogin = () => {
	saveLasePage()
	uni.navigateTo({
		url: '/pages_category_page2/userModule/login'
	})
}

const saveLasePage = () => {
	const pages = getCurrentPages();
	const currentPage = pages[pages.length - 1]
	uni.setStorageSync('last_page', currentPage.route);
	currentPage.options && uni.setStorageSync('last_page_options',
		currentPage.options)
}

const checkLogin = () => {
	if (!isLogin()) {
		jumpLogin()
		return !1;
	}
	return 1
}



const app = createApp(App)

app.use(uView)
app.use(store)
app.use(loadingPlugin)
app.component('GlobalLoading', GlobalLoading)
app.component('SlideLoading', SlideLoading)
app.component('SubmitButton', SubmitButton)
app.component('UvSkeleton', UvSkeleton)
app.component('ReturnTop', ReturnTop)
app.config.productionTip = false

app.provide('$store', store)
app.provide('$jump', jump)
app.provide('$jumpToTabbar', jumpToTabbar)
app.provide('$goBack', goBack)
app.provide('$getJumpParam', getJumpParam)
app.provide("$goBqCenter", $goToBqCenter)
app.provide("$goExternalLink", goExternalLink)
app.mount('#app')